package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.BizException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 添加班级
     *
     * @param clazz
     */
    @Override
    public void addClazz(Clazz clazz) {
        if (clazz.getBeginDate().isAfter(clazz.getEndDate())){
            throw new BizException("时间不合法,请重新选择");
        }
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);
    }

    /**
     * 查询全部班级
     *
     * @return
     */
    @Override
    public List<Clazz> selectClazzs() {
        List<Clazz> clazzList = clazzMapper.selectClazzs();
        return clazzList;
    }

    /**
     * 分页查询班级
     *
     * @param page
     * @return
     */
    @Override
    public PageBean clazzPagequery(ClazzQueryParam page) {
        PageHelper.startPage(page.getPage(), page.getPageSize());
        List<Clazz> clazzList = clazzMapper.selectClazzsPage(page);
        if(CollectionUtils.isEmpty(clazzList)){
            return new PageBean();
        }
        //给clazz中的status 赋值
        clazzList.forEach(clazz -> {
            LocalDate beginDate = clazz.getBeginDate();
            LocalDate endDate = clazz.getEndDate();
            if (beginDate.isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            } else if (beginDate.isBefore(LocalDate.now()) && endDate.isAfter(LocalDate.now())) {
                clazz.setStatus("已开班");
            } else {
                clazz.setStatus("已结课");
            }
        });
        Page<Clazz> clazzPage = (Page<Clazz>) clazzList;

        return new PageBean(clazzPage.getTotal(), clazzList);
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        Clazz clazz = clazzMapper.selectClazzById(id);
        return clazz;
    }

    /**
     * 修改班级信息
     *
     * @param clazz
     */
    @Override
    public void updateClazzById(Clazz clazz) {
        if (clazz.getBeginDate().isAfter(clazz.getEndDate())){
            throw new BizException("时间不合法,请重新选择");
        }
        clazzMapper.updateClazzById(clazz);
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void deleteClazz(Integer id) {
        //查询班级下是否有学生
        List<Student> studentList = studentMapper.selectStuByClazzId(id);
        if (!CollectionUtils.isEmpty(studentList)) {
            throw new BizException("对不起, 该班级下有学生, 不能直接删除");
        }
        clazzMapper.deleteClazzById(id);
    }
}
